/*
 * DesktopUtils.java
 *
 *  created: 10.10.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.util;

import cz.mp.k3bg.log.LoggerManager;
import cz.mp.util.FileUtils;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import javax.swing.SwingWorker;

/**
 *
 * @author Martin Pokorný
 * @version 0.1
 */
public class DesktopUtils {

    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(FileUtils.class, DEBUG);
    
    private static final boolean DESKTOP_SUPPORTED;
    private static Desktop desktop = null;

    static {
        DESKTOP_SUPPORTED = Desktop.isDesktopSupported();
        if (DESKTOP_SUPPORTED) {
            desktop = Desktop.getDesktop();
        }
    }

    /** */
    private DesktopUtils() {
    }

    /**
     *
     * @return
     */
    public static boolean isIsDesktopSupported() {
        return DESKTOP_SUPPORTED;
    }

    // TODO ! v Javě pro Windows XP je bug, který způsobuje, že Desktop.open(...) občas (ne některých konkrétních instalacích os) nefunguje správně.
    /**
     * Otevře soubor v programu přidruženém operačním systémem.
     *
     * @param path
     * @throws IllegalStateException
     * @see #isIsDesktopSupported()
     */
    public static void openFile(final String path) {
        logger.fine("file path = " + path);
        
        if (DESKTOP_SUPPORTED && desktop != null) {            
            new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    try {
                        desktop.open(new File(path));
                    } catch (IOException ex) {
                        // nic
                        logger.warning(ex.toString());
//                        throw ex;
                    } catch (IllegalArgumentException ex) {
                        // nic
                        logger.warning(ex.toString());
//                        throw ex;
                    } catch (Exception ex) {
                        // nic
                        logger.warning(ex.toString());
                        throw ex;
                    }                    
                    return null;
                }
            }.execute();
        }
        else {
            logger.warning("no desktop supported");
            throw new IllegalStateException("no desktop supported");
        }
    }

    /**
     *
     * @param url
     */
    public static void openBrowser(final String url)
            throws MalformedURLException {
//        logger.fine("url = " + url);
        URL urlUrl = new URL(url);
        openBrowser(urlUrl);
    }
    
    /**
     * 
     * @param url 
     */
    public static void openBrowser(final URL url) {
//        logger.fine("url = " + url.toString());
        if (DESKTOP_SUPPORTED && desktop != null) {
            new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    try {
                        desktop.browse(url.toURI());
                    } catch (IOException ex) {
                        // nic
                        logger.warning(ex.toString());
                    } catch (IllegalArgumentException ex) {
                        // nic
                        logger.warning(ex.toString());
                    }
                    return null;
                }
            }.execute();            
        }
        else {
            logger.warning("no desktop supported");
            throw new IllegalStateException("no desktop supported");
        }            
    }


}   // DesktopUtils
